package lz.controls
{
	import flash.display.SimpleButton;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.events.PageEvent;

	/**
	 *分页组件 
	 * @author lxh
	 * 
	 */	
	public class PageComponent extends EventDispatcher
	{
		private var _maxPages      :int = 1;//最大页数
		
		private var _curPage       :int = 1;//当前页数
		
		private var _totalNumbers  :int;//总记录数
		private var _pageSize      :int;//每一页显示的行数
		
		public var preBtn :SimpleButton ;//上一页
		public var nextBtn :SimpleButton;//下一页
		public var txtCurPage :TextField;//当前页
		public var txtMaxPages :TextField;//最大页
		
		private var _pageCompView :Object;
		
		public function PageComponent()
		{
			super();
		}
		
		public function initUI(viewObj :Object):void
		{
			_pageCompView 	= viewObj;
			preBtn 			= _pageCompView.preBtn;
			nextBtn			= _pageCompView.nextBtn;
			txtCurPage 		= _pageCompView.txtCurPage;
			txtMaxPages 	= _pageCompView.txtMaxPages;
			if(txtCurPage)
			{
				txtCurPage.mouseEnabled = false;
			}
			if(txtMaxPages)
			{
				txtMaxPages.mouseEnabled = false;
			}
			addEvtListener();
		}
		
		/**
		 *注册事件 
		 * 
		 */		
		private function addEvtListener():void
		{
			preBtn.addEventListener(MouseEvent.CLICK,onPre_clickHandler);
			nextBtn.addEventListener(MouseEvent.CLICK,onNext_clickHandler);
		}
		/**
		 *删除事件 
		 * 
		 */		
		private function removeEvtListener():void
		{
			
		}
		
		private function initPage():void
		{
			maxPages =  (totalNumbers % pageSize)==0?(totalNumbers / pageSize):(totalNumbers / pageSize + 1);
		}
		
		public function set totalNumbers(value:int):void
		{
			_totalNumbers = value;
			initPage();
		}
		
		public function get totalNumbers():int
		{
			return _totalNumbers;
		}
		
		public function set pageSize(value:int):void
		{
			_pageSize = value;
			initPage();
		}
		
		public function get pageSize():int
		{
			return _pageSize;
		}
		
		public function set curPage(value:int):void
		{
			_curPage = value;
			if(txtCurPage)txtCurPage.text = "" + _curPage;
			this.dispatchEvent(new PageEvent(PageEvent.PAGE_CHANGE,_curPage));
		}
		
		public function get curPage():int
		{
			return _curPage;
		}
		
		public function set maxPages(value:int):void
		{
			_maxPages = value<=0?1:value;
			if(txtMaxPages)txtMaxPages.text = "/ " + _maxPages;
		}
		
		public function get maxPages():int
		{
			return _maxPages;
		}
		/**
		 * 首页
		 * @param event
		 * 
		 */		
		protected function onFirst_clickHandler(event:MouseEvent):void
		{
			if(curPage!=1)
			{
				curPage = 1;
			}
		}
		
		/**
		 * 上一页
		 * @param event
		 * 
		 */		
		protected function onPre_clickHandler(event:MouseEvent):void
		{
			if(curPage>1)
			{
				curPage --;
			}
		}
		/**
		 * 下一页
		 * @param event
		 * 
		 */		
		protected function onNext_clickHandler(event:MouseEvent):void
		{
			if(curPage<maxPages)
			{
				curPage ++;
			}
		}
		
		/**
		 * 尾页
		 * @param event
		 * 
		 */		
		protected function onLast_clickHandler(event:MouseEvent):void
		{
			if(curPage!=maxPages)
			{
				curPage = maxPages;
			}
		}
	}
}